package com.industrial.governance.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.industrial.governance.domain.WordData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.industrial.governance.mapper.DgQuantityQualityManageMapper;
import com.industrial.governance.domain.DgQuantityQualityManage;
import com.industrial.governance.service.IDgQuantityQualityManageService;

import javax.servlet.http.HttpServletResponse;

/**
 * 数据质量管理Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-03-01
 */
@Service
public class DgQuantityQualityManageServiceImpl implements IDgQuantityQualityManageService {
    @Autowired
    private DgQuantityQualityManageMapper dgQuantityQualityManageMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询数据质量管理
     *
     * @param baseId 数据质量管理主键
     * @return 数据质量管理
     */
    @Override
    public DgQuantityQualityManage selectDgQuantityQualityManageByBaseId(String baseId) {
        return dgQuantityQualityManageMapper.selectDgQuantityQualityManageByBaseId(baseId);
    }

    /**
     * 查询数据质量管理列表
     *
     * @param dgQuantityQualityManage 数据质量管理
     * @return 数据质量管理
     */
    @Override
    public List<DgQuantityQualityManage> selectDgQuantityQualityManageList(DgQuantityQualityManage dgQuantityQualityManage) {
        List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(dgQuantityQualityManage);
        for (DgQuantityQualityManage temp : dgQuantityQualityManages) {
            String county = temp.getCounty();
            String countyName = redisTemplate.opsForValue().get("area:" + county);
            temp.setSource(countyName + temp.getSource());
        }
        return dgQuantityQualityManages;
    }

    /**
     * 新增数据质量管理
     *
     * @param dgQuantityQualityManage 数据质量管理
     * @return 结果
     */
    @Override
    public int insertDgQuantityQualityManage(DgQuantityQualityManage dgQuantityQualityManage) {
        return dgQuantityQualityManageMapper.insertDgQuantityQualityManage(dgQuantityQualityManage);
    }

    /**
     * 修改数据质量管理
     *
     * @param dgQuantityQualityManage 数据质量管理
     * @return 结果
     */
    @Override
    public int updateDgQuantityQualityManage(DgQuantityQualityManage dgQuantityQualityManage) {
        return dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
    }

    /**
     * 批量删除数据质量管理
     *
     * @param baseIds 需要删除的数据质量管理主键
     * @return 结果
     */
    @Override
    public int deleteDgQuantityQualityManageByBaseIds(String[] baseIds) {
        return dgQuantityQualityManageMapper.deleteDgQuantityQualityManageByBaseIds(baseIds);
    }

    /**
     * 删除数据质量管理信息
     *
     * @param baseId 数据质量管理主键
     * @return 结果
     */
    @Override
    public int deleteDgQuantityQualityManageByBaseId(String baseId) {
        return dgQuantityQualityManageMapper.deleteDgQuantityQualityManageByBaseId(baseId);
    }

    /**
     * 生成数据质量报告
     *
     * @param currentYear
     */
    @Override
    public Map<String, Object> genDataWord(int currentYear, HttpServletResponse response) {
        List<DgQuantityQualityManage> list = dgQuantityQualityManageMapper.selectGenDataList(String.valueOf(currentYear - 1));
        List<WordData> wordList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            Map<String, List<DgQuantityQualityManage>> resultMap = list.stream().collect(Collectors.groupingBy(DgQuantityQualityManage::getRuleName));
            for (Map.Entry<String, List<DgQuantityQualityManage>> entry : resultMap.entrySet()) {
                WordData tempWordData = new WordData();
                String key = entry.getKey();
                tempWordData.setTitle(key);
                StringBuilder sb = new StringBuilder();
                List<DgQuantityQualityManage> value = entry.getValue();
                if (value != null && value.size() > 0) {
                    value.forEach(item -> {
                        String countyName = redisTemplate.opsForValue().get("area:" + item.getCounty());
                        String cityName = redisTemplate.opsForValue().get("area:" + item.getCity());
                        sb.append(cityName).append("-").append(countyName).append(item.getSource()).append("异常数量:").append(item.getFailCount()).append(",");
                    });
                }
                tempWordData.setContents(sb.deleteCharAt(sb.length() - 1).toString());
                wordList.add(tempWordData);
            }
        }
        Map<String, Object> reMap = new HashMap<>(16);
        reMap.put("years", String.valueOf(currentYear - 1));
        reMap.put("contents", wordList);
        return reMap;
    }
}
